package ch.fhnw.qualiscan.application.modals;

import ch.fhnw.qualiscan.Config;
import ch.fhnw.qualiscan.application.models.SurveyModel;
import ch.fhnw.qualiscan.application.views.EditSurveyCoordinatorView;
import ch.fhnw.qualiscan.core.Modal;
import ch.fhnw.qualiscan.core.db.pojos.Survey;
import ch.fhnw.qualiscan.core.db.pojos.SurveyUserRole;
import ch.fhnw.qualiscan.core.helpers.EmailHelper;
import ch.fhnw.qualiscan.core.helpers.StageHelper;
import com.vaadin.data.Property;
import com.vaadin.data.Property.ValueChangeEvent;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.OptionGroup;
import com.vaadin.ui.RichTextArea;
import com.vaadin.ui.VerticalLayout;
import eu.livotov.tpt.i18n.TM;
import java.util.List;

@SuppressWarnings("serial")
public class NextStageModal extends Modal {
	
	private Survey survey;
	private StageHelper stageHelper;
	private EditSurveyCoordinatorView caller;
	private RichTextArea editor;
	private SurveyModel surveyModel;
	private OptionGroup sendEmail;
	private boolean doSendEmail = false;
	private ClickEvent button;
	
	public NextStageModal(Survey survey, EditSurveyCoordinatorView caller, ClickEvent button) {
		this.survey = survey;
		this.caller = caller;
		this.button = button;
		
		stageHelper = new StageHelper();
		surveyModel = new SurveyModel();
		buildLayout();
	}
	
	public void buildLayout() {
		Label descr;
		if(caller.stage == 3) {
			setCaption(TM.get("nextstage.title2"));
			descr = textHelper.p(TM.get("nextstage.description2"));
		} else {
			setCaption(TM.get("nextstage.title"));
			descr = textHelper.p(TM.get("nextstage.description"));
			
			final VerticalLayout mailWrap = new VerticalLayout();
			mailWrap.setSpacing(true);
			mailWrap.setVisible(false);
			
			// editor
			editor = new RichTextArea();
			editor.setValue(TM.get("email.stagechange.text", survey.getTitle()));
			editor.setWidth("100%");
			mailWrap.addComponent(editor);
			
			// email checkbox
			sendEmail = new OptionGroup();
			sendEmail.addItem(1);
			sendEmail.setItemCaption(1, TM.get("editsurvey.stagechange.sendmail"));
			sendEmail.setMultiSelect(true);
			sendEmail.setImmediate(true);
			sendEmail.addListener(new Property.ValueChangeListener() {
				public void valueChange(ValueChangeEvent event) {
					
					if (event.getProperty().getValue().toString().equals("[1]")) {
						doSendEmail = true;
						mailWrap.setVisible(true);
					} else {
						doSendEmail = false;
						mailWrap.setVisible(false);
					}
				}
			});
			addComponent(sendEmail);
			addComponent(mailWrap);
		}

		addComponent(descr);

		HorizontalLayout buttonBar = new HorizontalLayout();
		buttonBar.setSpacing(true);
		buttonBar.setMargin(true, false, false, false);
		
		Button confirmButton = new Button(TM.get("action.confirm"), new Button.ClickListener() {
			public void buttonClick(ClickEvent event) {
				
				stageHelper.nextStage(survey);
				if (doSendEmail == true) {
					//Emails versenden
					EmailHelper eh = new EmailHelper();
					List<SurveyUserRole> surList = surveyModel.getSurListForSurvey(survey);
					for(int i = 0; i < surList.size(); i++) {
						eh.sendEmail(surList.get(i).getUser().getEmail(), TM.get("email.stagechange.subject", surList.get(i).getSurvey().getTitle()), editor.getValue().toString() + "<br /><br />" + Config.LANDING_URL);
					}
				}
				caller.buildLayout();
				NextStageModal nsm = (NextStageModal) event.getButton().getData();
				nsm.close();
				button.getButton().setEnabled(false);
			}
		});
		confirmButton.setIcon(Config.ICON_SAVE);
		confirmButton.setData(this);
		
		Button cancelButton = new Button(TM.get("action.cancel"), new Button.ClickListener() {
			public void buttonClick(ClickEvent event) {
				NextStageModal nsm = (NextStageModal) event.getButton().getData();
				nsm.close();
			}
		});
		cancelButton.setIcon(Config.ICON_CANCEL);
		cancelButton.setData(this);
		
		buttonBar.addComponent(confirmButton);
		buttonBar.addComponent(cancelButton);
		this.addComponent(buttonBar);
	}
}
